<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .p {
            width: 300px;
            height: 300px;
            background-color: lightblue;
        }

        .s {
            width: 200px;
            height: 100px;
            background-color: deeppink;
        }
    </style>
</head>

<body>
    <!-- 事件冒泡顺序: 由内到外 -->
    <div class="p">
        <div class="s"></div>
    </div>
    <script>
        // 获取元素dom对象
        var p = document.getElementsByClassName('p')[0]
        var s = p.getElementsByClassName('s')[0]

        s.onclick = function (event) {
            // 阻止事件冒泡
            event.stopPropagation()
            console.log('子元素被点击了');
        }
        p.onclick = function () {
            console.log('父元素被点击了');
        }
        document.body.onclick = function () {
            console.log('body点击事件被触发了');
        }
    </script>
</body>

</html>